package com.chixing.day03_oop.work;

public class Work4 {
    public static void main(String[] args) {
        Product p1=new Product();
        p1.setProductId(1);
        p1.setProductName("西瓜");
        p1.setProductPrice(100);
        p1.setProductAddress("苏州");

        Product p2=new Product();
        p2.setProductId(2);
        p2.setProductName("榴莲");
        p2.setProductPrice(500);
        p2.setProductAddress("徐州");

        Product p3=new Product();
        p3.setProductId(3);
        p3.setProductName("草莓");
        p3.setProductPrice(200);
        p3.setProductAddress("扬州");

        Product p4=new Product();
        p4.setProductId(4);
        p4.setProductName("苹果");
        p4.setProductPrice(400);
        p4.setProductAddress("常州");

        Product p5=new Product();
        p5.setProductId(5);
        p5.setProductName("凤梨");
        p5.setProductPrice(600);
        p5.setProductAddress("上海");

        Product p6=new Product();
        p6.setProductId(6);
        p6.setProductName("菠萝");
        p6.setProductPrice(600);
        p6.setProductAddress("北京");

        Product p7=new Product();
        p7.setProductId(7);
        p7.setProductName("提子");
        p7.setProductPrice(300);
        p7.setProductAddress("南京");

        Product p8=new Product();
        p8.setProductId(8);
        p8.setProductName("葡萄");
        p8.setProductPrice(400);
        p8.setProductAddress("哈尔滨");

        Product p9=new Product();
        p9.setProductId(9);
        p9.setProductName("火龙果");
        p9.setProductPrice(400);
        p9.setProductAddress("齐齐哈尔");

        Product p10=new Product();
        p10.setProductId(10);
        p10.setProductName("甘蔗");
        p10.setProductPrice(600);
        p10.setProductAddress("镇江");

        /**
         * 3. 实例化10个商品添加到数组Product[] 中，并遍历输出商品的id与名称
         */
        Product[] products={p1,p2,p3,p4,p5,p6,p7,p8,p9,p10};
        System.out.println("遍历输出");
        for (int i = 0; i < products.length; i++) {
            System.out.println("商品编号: "+products[i].getProductId()+" 商品名称: "+products[i].getProductName());
        }

        /**
         * 4. 输出显示商品价格超过300的商品的名称
         */
        System.out.println("输出显示商品价格超过300的商品的名称");
        for (int i = 0; i < products.length; i++) {
            if(products[i].getProductPrice()>300)
                System.out.println("商品名称: "+products[i].getProductName()+" 商品价格: "+products[i].getProductPrice());
        }

        /**
         * 5. 按照商品价格从低到高，将商品重新排序
         */
        System.out.println("按照商品价格从低到高，将商品重新排序");
        for (int i = 0; i < products.length-1; i++) {
            for (int j = 0; j < products.length-i-1; j++) {
                if(products[j].getProductPrice()>products[j+1].getProductPrice()){
                    Product temp=products[j];
                    products[j]=products[j+1];
                    products[j+1]=temp;
                }
            }
        }
        for (int i = 0; i < products.length; i++) {
            System.out.println("商品名称: "+products[i].getProductName()+" 商品价格: "+products[i].getProductPrice());
        }
    }
}
